<?php

namespace Store\Controller;

use Think\Controller;

class CommonController extends Controller {

    public function _initialize() {

        if (CONTROLLER_NAME == 'Store') {
            $this->checkUserSession();
        }
    }

    //验证用户是否登录
    public function checkUserSession() {
        //if (!session('USER')) {
        if (!session('_wxsuserid')) {
            echo '<script>window.location.href="' . __APP__ . '/Index/login"</script>';
        }
    }

    //生成系统时间
    protected function crtTime() {
        return date('Y-m-d H:i:s');
    }

    function getStoreNameById($id) {
        $this->assign('title', M('Score_store')->where('id=' . $id)->getField('title'));
    }
    
    
    


    function getQrproductByQid($id,$card_no=null) {
        $mod = D('QrcodeproductView');
        $data = $mod->where('Score_qrcode.id=' . $id)->select();
       //print_r($data);exit;
        //==echo $mod->getLastSql();exit;
        foreach ($data as $key => $value) {
            //$str .= $value['title'].'×'.$value['num'] . '、';
            $str .= $value['title'].'×'.$value['num']. '&nbsp,&nbsp';
            $pids.= $value['pid'] . '&nbsp,&nbsp';
            if($value['issale'] == 1){
                $SALE += ($value['num']*$value['p_score'])*getSaleByCardNo($card_no);
               //;
            }else{
                $NO_SALE+=$value['p_score']*$value['num'];
            }
        }
      
        //__ROOT__/api.php/Qrcode?value={$url}&size=12
        //$this->assign($str)
      //  echo  $SALE+$NO_SALE;exit;
        $this->assign('pid', rtrim($pids, '&nbsp,&nbsp'));
        $this->assign('product', rtrim($str, '&nbsp,&nbsp'));
        if($card_no){
            $this->assign('score', $this->scoreReplace($SALE+$NO_SALE)/*round($SALE+$NO_SALE,-2)*/);
        }else{
            $this->assign('score', $data[0]['score'] );
        }
        $this->assign('price', $data[0]['price']);
    }

    function orderDetail($id) {
        $mod = M('Score_order');
        $data = $mod->where('id=' . $id)->find();
        $this->assign('data', $data);
        $this->assign('img', getWxBaseInfo($data['openid']));
    }
    //积分价格处理
    function scoreReplace($score){ 
        $str = substr($score, -2);
        if($str ==0 || $str == 50){
            $ll  = $score;
        }else{
            if($str<50 ){
                $ll = str_replace($str,50,$score);
            }else{
                $ll = $score+(100-$str);
            }
        }
        return $ll;
    }

}

?>